package com.wetao.lib_common.view.date;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.TextView;

import com.wetao.lib_common.R;

import java.lang.reflect.Field;


/**
 * Created by Administrator on 2017/10/18.
 * 日期选择
 */

public class CustomDatePickerDialog implements View.OnClickListener {
    private final Context context;
    private Dialog dialog;
    private DatePicker dp;
    private OnDateSetListener listener;
    private boolean hideDate; //是否隐藏选择日期

    public CustomDatePickerDialog(Context context, int style) {
        this.context = context;
        initDialog(context, style);
    }

    public CustomDatePickerDialog(Context context,int style,boolean hideDate){
        this.context = context;
        this.hideDate = hideDate;
        initDialog(context,style);
    }


    private void initDialog(Context context, int style) {
        dialog = new Dialog(context, style);
        dialog.setContentView(R.layout.common_date_picker);

        TextView tvOk =  dialog.findViewById(R.id.yes_btn);
        TextView tvCancel =  dialog.findViewById(R.id.no_btn);
        tvCancel.setOnClickListener(this);
        tvOk.setOnClickListener(this);

        dp =  dialog.findViewById(R.id.date_picker);

        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = window.getAttributes();
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        lp.width = (int) (metrics.widthPixels * 0.5);
        window.setAttributes(lp);

        if (hideDate){ //是否隐藏日期选择
            hideDay(dp);
        }


    }

    public void show() {
        if (dialog != null) {
            dialog.show();
        }
    }

    public void dismiss() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }


    @Override
    public void onClick(View v) {
        if (listener == null) {
            return;
        }

        if (v.getId() == R.id.yes_btn){
            listener.onDateSet(dp,dp.getYear(),dp.getMonth(), dp.getDayOfMonth(),hideDate);
            dismiss();
        }else if (v.getId() == R.id.no_btn){
            dismiss();
        }
    }


    /**
     * 隐藏日期选项
     * @param mDatePicker
     */
    private void hideDay(DatePicker mDatePicker) {
        try {
            /* 处理android5.0以上的特殊情况 */
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                int daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
                if (daySpinnerId != 0) {
                    View daySpinner = mDatePicker.findViewById(daySpinnerId);
                    if (daySpinner != null) {
                        daySpinner.setVisibility(View.GONE);
                    }
                }
            } else {
                Field[] datePickerfFields = mDatePicker.getClass().getDeclaredFields();
                for (Field datePickerField : datePickerfFields) {
                    if ("mDaySpinner".equals(datePickerField.getName()) || ("mDayPicker").equals(datePickerField.getName())) {
                        datePickerField.setAccessible(true);
                        Object dayPicker = new Object();
                        try {
                            dayPicker = datePickerField.get(mDatePicker);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (IllegalArgumentException e) {
                            e.printStackTrace();
                        }
                        ((View) dayPicker).setVisibility(View.GONE);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public void setOnDateSelectedListener(OnDateSetListener listener) {
        this.listener = listener;
    }

    public interface OnDateSetListener {
        void onDateSet(DatePicker datePicker, int year, int month, int day);
        void onDateSet(DatePicker datePicker, int year, int month, int day,boolean hide);

    }
}
